<?php

class SteamUserController extends ModuleController {
    
    public function init() {
        ;
    }
    /**
     * @paths: steamprofile
     * @components: navigation, loginblock, steamlogin
     */
    public function steamprofile() {
        $pagedata = array();
        
        $curl = curl_init('http://localhost:8082/?action=getprofile&steamid=76561198081180454');
        
       // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        //curl_setopt($curl, CURLOPT_HEADER, false);
       // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        
        
        $result = curl_exec($curl); // run the whole process  
        curl_close($curl);   
               

        $pagedata['profile'] = json_decode($result, true);
        $this->output($pagedata);
        return OK;
    }   
    
    
    /**
     * @paths: steamloginrequest
     */
    public function steamloginrequest() {
        //$this->load->library('lightopenid');

        $openid = new LightOpenID('http://localhost/pleasure/');

        if (!$openid->mode) {
            
            $openid->returnUrl = "http://localhost/pleasure/steamlogin";
            $openid->identity = 'http://steamcommunity.com/openid';
            $openid->required = array('ext0' => 'contact/email',
                'ext1' => 'namePerson',
                'ext2' => 'namePerson/first',
                'ext3' => 'namePerson/last'
            );

            $openid->optional = array('ext4' => 'namePerson/friendly',
                'ext5' => 'contact/city/home',
                'ext6' => 'contact/state/home',
                'ext7' => 'contact/web/default',
                'ext8' => 'media/image/aspect11',
            );

            header('Location: ' . $openid->authUrl());
        }
        return OK;
    }
    
    /**
     * @paths: steamlogin
     * @mode: non-page 
     */
    public function steamlogin() {
        $user = $this->user;
        $data = array();
        $next_action = $this->registry->router->getAction('');
        if ($user->isLogged) {
            //echo"Уже залогинен!<br/>";
            $user->fireEvent('message', array('message' => "Вы уже залогинены на сайте."));
            return $next_action;
        }

        $data['login'] = $_GET['openid_identity'];
        $data['strategy'] = "Steam";
        if($user->doLogin($data))
        {
            $this->user->fireEvent('message', array('message' => "Вы успешно вошли на сайт"));
        }
        else
        {
            $this->user->fireEvent('message', array('message' => "Ошибка входа"));
        }
       
        return $next_action;
    }
}
?>
